package org.hive.hive;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import org.apache.http.client.HttpClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.commons.io.*;

import org.json.*;
/*import net.sf.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;*/

public class Login extends Activity implements OnClickListener {
	Button login;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
         
        login=(Button)findViewById(R.id.login);
        login.setOnClickListener(this);
}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		EditText usernameText = (EditText)findViewById(R.id.editText1);
		EditText passwordText = (EditText)findViewById(R.id.editText2);
		String tempUserName = usernameText.getText().toString();
		String tempPassword = passwordText.getText().toString();
		Global.albumlist = new HashMap();
		Global.takenPhotos = new HashMap();
		Global.recoPhotos = new HashMap();
		String URL = Global.URL+"MobileUserAuth?username="+tempUserName+"&password="+tempPassword;
		//String URL = Global.URL+"MobileUserAuth?username=ndevanur@gmail.com&password=password";
		try{
			HttpClient hc = new DefaultHttpClient();
			HttpPost post = new HttpPost(URL);

			
			HttpResponse rp = hc.execute(post);
			InputStream in = rp.getEntity().getContent();
			String myString = IOUtils.toString(in);

			JSONObject myJSON = new JSONObject(myString);// = (JSONObject)JSONSerializer.toJSON(myString);
			Global.userid = myJSON.getInt("userid");
			if ( Global.userid != -1)
			{
				JSONArray albums = myJSON.getJSONArray("albums");
				JSONArray photos = myJSON.getJSONArray("takenPhotos");
				JSONArray recos = myJSON.getJSONArray("recoPhotos");
				for ( int i = 0 ; i<albums.length()-1;i++)
				{
					JSONObject tempObj = albums.getJSONObject(i);
					int tempAlbumId = tempObj.getInt("id");
					String tempAlbumName = tempObj.getString("name");
					//Albums tempAlbum = new Albums();
					//Global.albumlist.add(tempAlbumId,tempAlbumName);
					Global.albumlist.put(tempAlbumName,new Integer(tempAlbumId) );
				}
				for ( int j=0;j<photos.length()-1;j++)
				{
					JSONObject tempObj = photos.getJSONObject(j);
					photos tempPhoto = new photos();
					tempPhoto.photoid= tempObj.getInt("id");
					tempPhoto.albumid = tempObj.getInt("albumid");
					tempPhoto.lat = (float)tempObj.getDouble("lat");
					tempPhoto.lon= (float)tempObj.getDouble("lon");
					tempPhoto.owner_id = Global.userid;
					Global.takenPhotos.put(new Integer(tempPhoto.photoid), tempPhoto);


				}
				for ( int k = 0; k < recos.length()-1;k++)
				{
					JSONObject tempObj = recos.getJSONObject(k);
					photos tempPhoto = new photos();
					tempPhoto.photoid = tempObj.getInt("id");
					tempPhoto.albumid = tempObj.getInt("albumid");
					tempPhoto.owner_id = tempObj.getInt("owner_id");
					tempPhoto.lat = (float)tempObj.getDouble("lat");
					tempPhoto.lon = (float)tempObj.getDouble("long");
					Global.recoPhotos.put(new Integer(tempPhoto.photoid), tempPhoto);
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		if ( Global.userid != -1)
		{
			Intent i;
			i=new Intent(this,GeoTag.class);
			startActivity(i);
		}
	}
	
}
